package org.bluedream.core.module.login.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.bluedream.comm.base.BaseServiceImpl;
import org.bluedream.comm.base.TreeServiceImpl;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.core.module.service.clsItem.ClsItemService;
import org.bluedream.core.module.sys.dao.UserDao;
import org.bluedream.core.module.sys.entity.Menu;
import org.bluedream.core.module.sys.entity.Organization;
import org.bluedream.core.module.sys.entity.Role;
import org.bluedream.core.module.sys.entity.User;
import org.bluedream.core.module.sys.service.MenuService;
import org.bluedream.core.utils.SpringUtil;
import org.bluedream.core.utils.UserUtil;
import org.bluedream.core.utils.YmlRead;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@DS("master")
@Transactional(
        readOnly = true
)
public class LoginService extends BaseServiceImpl<UserDao, User> implements IService<User> {
    private static final Logger logger = LoggerFactory.getLogger(LoginService.class);
    @Resource
    private MenuService menuService;

    /**
     * 验证shiro CustomRealm中传入的登录用户名是否存在
     * @param arg0
     * @return
     * @throws NullPointerException
     */
    public User getLoginUser(String arg0) throws NullPointerException{
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("login_code" , arg0);
        return baseMapper.selectOne(qw);
    }

/*    public User getLoginUser(String arg0 , String orgCode) throws NullPointerException{
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("login_code" , arg0);
        User u = baseMapper.selectOne(qw);
        if (u != null){
            u.setOrgCode(orgCode);
            baseMapper.updateUserLoginOrg(u);
        }
        return u;
    }*/

/*    public boolean updateUserLoginOrg(User user){
        return  baseMapper.updateUserLoginOrg(user);
    }*/

/*    public List<Role> getRoleList(User user) throws NullPointerException {
        return baseMapper.getRoles(user);
    }*/

    /**
     * 获取用户的角色ID集合，传入 shiro CustomRealm中的权限管理器
     * @param user
     * @return
     * @throws NullPointerException
     */
    public List<String> getRoleIds(User user)throws NullPointerException {
        return baseMapper.getRoleIds(user);
    }

/*    public List<Menu> getMenuPermissions(User user) throws NullPointerException{
        return baseMapper.getMenuPermissions(user);
    }*/

    /**
     * 获取用户所拥有的菜单权限ID集合 传入 shiro CustomRealm中的权限管理器
     * @param user
     * @return
     * @throws NullPointerException
     */
    public List<String> getMenuPermissions2String(User user) throws NullPointerException{
        return baseMapper.getMenuPermissions2String(user);
    }

    public List<Map<String , Object>> menuTreeData(){
        return menuService.getMenuTreeDataByLoginUserAndOrg(0);
    }

/*    public List<Organization> getOrganizations(User user)throws NullPointerException{
        return baseMapper.getOrganizations(user);
    }*/

/*    public boolean checkLoginOrg(String loginCode){
        if (loginCode.equals("system")){
            return true;
        }
        String orgId =  baseMapper.checkLoginOrg(loginCode);
        if (EmptyUtil.isNoEmpty(orgId)){
            return true;
        }
        return false;
    }*/

    /**
     * 该方法用于验证登录用户是否拥有登录该组织的权限
     * @param loginCode
     * @param orgCode
     * @return
     */
    public User checkLoginValidityByOrg(String loginCode , String orgCode){
        if (!UserUtil.isSystem(loginCode)){
            List<String> loginOrgIds = baseMapper.checkLoginOrg(loginCode , orgCode);
            if (loginOrgIds.size() == 0){
                return null;
            }
        }
        return this.getLoginUser(loginCode);
    }

/*    public boolean checkLoginOrg(String loginCode , String orgCode){
        if (loginCode.equals("system")){
            return true;
        }

        List<String> loginOrgIds = baseMapper.checkLoginOrg(loginCode , orgCode);
        if (loginOrgIds.size() == 1){
            return true;
        }
        return false;
    }*/

/*    public List<String> getOrganizationIds(User user)throws NullPointerException{
        if (user.getLoginCode().equals("system")){
            return baseMapper.getOrganizationIds();
        }
        return baseMapper.getOrganizationIds(user);
    }*/
}
